#include "ff.h"
#include "lv_demos/src/lv_demo_widgets/lv_demo_widgets.h"
#include "lv_drivers/win32drv/win32drv.h"
#include "lvgl/lvgl.h"
#include "test/lua_test.h"
#include <stdlib.h>
#include <unistd.h>
#include <windows.h>

static void hal_init(void);
static int tick_thread(void *data);

void task_callback()
{
    static uint8_t cnt = 0;
    printf("time out %d\r\n", cnt++);
}

void lv_time_test()
{
    lv_timer_t *task_timer = lv_timer_create(task_callback, 200, NULL);
}

lv_obj_t *lv_create()
{
    lv_obj_t *lv_obj = lv_arc_create(lv_scr_act());
}

int FatFsTest()
{
    FATFS fatfs;
    FATFS *fs = &fatfs;
    FRESULT res;
    FIL fil;
    UINT bw = 0;

    BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */

    MKFS_PARM mkfs = {
        FM_FAT32,
        0,
        0,
        0,
        0};

    //cout << "----------------------------------------------------------------" << endl;

    //res = f_mount(fs, "0:", 0);

    //if (res != FR_OK)
    //{
    //    cout << "mount failed! res: " << res << endl;
    //    //return -1;
    //}

    //cout << "----------------------------------------------------------------" << endl;

    ////MKFS_PARM opt = { FM_FAT32 };
    //BYTE Buff[FF_MAX_SS];			/* Working buffer */

    ////res = f_mkfs("0:", 0, 1);
    res = f_mkfs("0:", &mkfs, work, sizeof(work));
    ////res = f_mkfs("0:", 0, work, sizeof work);

    if (res != FR_OK)
    {
        printf("f_mkfs failed! res: %d\r\n", res);
        return -1;
    }

    printf("----------------------------------------------------------------\r\n");

    res = f_mount(fs, "0:", 1);

    if (res != FR_OK)
    {
        printf("f_mkfs failed! res: %d\r\n", res);
        //return -1;
    }

    printf("----------------------------------------------------------------\r\n");

    res = f_open(&fil, "0:\\test.txt", FA_CREATE_NEW | FA_WRITE);
    if (res != FR_OK)
    {
        printf("open failed! res: %d\r\n", res);
        return -2;
    }

    const char *str_write = "Hello From FATFS";
    //const char* str_write2 = "str_write2";

    res = f_write(&fil, str_write, strlen(str_write), &bw);
    if (res != FR_OK)
    {
        printf("write failed! res: %d\r\n", res);
        return -3;
    }

    printf("Write Num: %d\r\n", bw);
    f_close(&fil);

    printf("----------------------------------------------------------------\r\n");

    // res = f_open(&fil, "0:\\test.txt", FA_READ);
    // if (res != FR_OK)
    // {
    //     cout << "open failed! res: " << res << endl;
    //     return -4;
    // }

    // char str_read[100] = { 0 };

    // res = f_read(&fil, str_read, 100, &bw);
    // if (res != FR_OK)
    // {
    //     cout << "read failed! res: " << res << endl;
    //     return -5;
    // }
    // cout << "Read Num: " << bw << endl;

    // cout << "str_read: " << str_read << endl;
    // f_close(&fil);

    // cout << "----------------------------------------------------------------" << endl;

    // DWORD nclst = 0;
    // f_getfree("0:", &nclst, &fs);
    // cout << "f_getfree nclst == " << nclst << endl;

    // cout << "----------------------------------------------------------------" << endl;

    return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
    /*Initialize LittlevGL*/
    lv_init();

    /*Initialize the HAL for LittlevGL*/
    lv_win32_init(hInstance, SW_SHOWNORMAL, 360, 640, NULL);

    //lv_time_test();

    /*Run the demo*/
    //test();

    // lv_obj_t *lv_obj = lv_create();
    // lv_obj_align(lv_obj, LV_ALIGN_CENTER, 0, 0);

    call_lua();
    
    while (!lv_win32_quit_signal)
    {
        /* Periodically call the lv_task handler.
         * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();
        usleep(10000); /*Just to let the system breath*/
    }
    return 0;
}
